<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\Setting;
use App\Cascades;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Tree;
use App\Models\Category;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class CascadeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '学科页';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Cascades());

        // $grid->column('id', __('Id'));
        $grid->column('name', __('名称'));
        $grid->column('type', __('类型'))->using(['1' => '学段', '2' => '年级','3'=> '学科']);
        $grid->column('parent_id', __('Parent id'));
        $grid->column('is_default', __('Is default'));
        $grid->column('deleted_at', __('Deleted at'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    public function index(Content $content){
        $list = Cascades::with(['grade','subject'])->where('type',1)->get();

        // dump($list);

        return $content
        ->title('学科设置')
        ->view('cascades.index', compact('list'));
    }

    // public function index(Content $content)
    // {
    //     $tree = new Tree(new Cascades());
    //     $tree->branch(function($branch){
    //         switch($branch['type']){
    //             case 1:
    //                 $color = 'black';
    //                 $descrition = '学段';
    //             break;
    //             case 2:
    //                 $color = 'red';
    //                 $descrition = '年级';
    //             break;
    //             case 3:
    //                 $color = 'blue';
    //                 $descrition = '学科';
    //             break;
    //         }
    //         return "<span style=\"color:{$color};\">[$descrition]{$branch['id']}-{$branch['name']}</span>";
    //     });
    //     return $content
    //         ->header('学科关联')
    //         ->body($tree);
    // }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Cascades::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('type', __('Type'));
        $show->field('parent_id', __('Parent id'));
        $show->field('is_default', __('Is default'));
        $show->field('deleted_at', __('Deleted at'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Cascades());
        $form->radioCard('type', __('类型'))
            ->options(['1'=>'学段','2'=>'年级','3'=>'学科'])
            ->default('1')->when('2',function(Form $form){
                $form->select('parent_id', __('父级学段'))->options('/admin/cascades/getByMap/1/0');
            })->when('3',function(Form $form){
                $form->select('parent_id', __('父级学段'))->options('/admin/cascades/getByMap/1/0');
            });

        $form->text('name', __('名称'));

        return $form;
    }

    public function getCascades($type=1,$parent_id=0,Request $request){
        $q = $request->get('q',0);
        if(!empty($q))
            $parent_id = $q;
        $cascades = Cascades::where('type',$type)->where('parent_id',$parent_id)->get();
        $cascades = $cascades->reject(function($fight){
            $fight->text = $fight->name;
        });
        return $cascades;
    }

    public function setting(Content $content)
    {
        return $content
            ->title('网站设置')
            ->body(new Setting());
    }

    public function del($ids=''){
        if(empty(strlen($ids))){
            admin_warning('请先勾选要删除的学科数据！');
        }else{
            if(strlen($ids) > 1){
                $ids = explode(',',$ids);
            }
            Cascades::destroy($ids);
            admin_success('数据处理成功.');

        }
        // 处理完成之后回到原来的表单页面，或者通过返回`redirect()`方法跳转到其它页面
        return redirect('admin/cascades');
    }



    
}
